package jsp;

import util.CookieUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLDecoder;
import java.net.URLEncoder;

/**
 * @Author:石小俊
 * @Date:2022/12/13 13:35
 * @Version:1.0-SNAPSHOT
 * @Description:
 */
@WebServlet("*.cookie")
public class CookieServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String path = request.getServletPath();
        if("/add.cookie".equals(path)){
            addCookie(request,response);
        }
        if("/findAll.cookie".equals(path)){
            findAll(request,response);
        }
        if("/modify.cookie".equals(path)){
            modifyCookie(request,response);
        }
        if("/remove.cookie".equals(path)){
            removeCookie(request,response);
        }
        if("/findByName.cookie".equals(path)){
            findByName(request,response);
        }
    }

    public void findByName(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name = request.getParameter("name");
        String value = CookieUtil.findByName(name,request);
        System.out.println(name+"对应的Cookie数据为:"+value);
    }
    public void removeCookie(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 在Cookie方法中,没有提供删除的方法
        // 当Cookie生存时间结束后,自动销毁
        // 可以利用该特性实现删除功能
        // 修改指定key的cookie,其值随意,将其生存时间设置为0即表示删除指定key的cookie
//        Cookie cookie = new Cookie("password","");
//        cookie.setMaxAge(0);
//        response.addCookie(cookie);
        CookieUtil.removeCookie("password",response);
    }
    public void modifyCookie(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 在Cookie方法中,没有提供修改的方法
        // 但是,在保存Cookie数据的时候
        // 遇到相同的key的数据时,后面添加的会将前面添加的覆盖
        // 可以通过该特性实现修改
        // 修改的本质仍然是添加
//        Cookie cookie = new Cookie("password","123321");
//        response.addCookie(cookie);
        CookieUtil.modifyCookie("password","123321",response);
    }

    public void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取请求中所有的Cookie
        Cookie[] cookies = request.getCookies();
        for(Cookie cookie : cookies){
            System.out.println(cookie.getName()+":"+ URLDecoder.decode(cookie.getValue(),"UTF-8"));
        }
    }

    public void addCookie(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 创建一个Cookie对象
//        Cookie cookie = new Cookie("username","admin");
//        // 设置Cookie生存时间,单位:秒
//        cookie.setMaxAge(10);
//        Cookie cookie2 = new Cookie("password","123456");
//        String name = "管理员";
//        // 默认情况下,Cookie不支持中文,想要存储中文,必须使用Cookie支持的字符集
//        // 为了便于存储中文,Cookie提供了一个中文的转换工具
//        // 参数一:当前需要存储的中文字符
//        // 参数二:当前字符串的字符集是什么
//        name = URLEncoder.encode(name,"UTF-8");
//        Cookie cookie3 = new Cookie("name",name);
//        // 将Cookie对象添加到响应中
//        // 当服务器返回响应时,会将响应中的所有Cookie数据保存到计算机硬盘上
//        response.addCookie(cookie);
//        response.addCookie(cookie2);
//        response.addCookie(cookie3);

        CookieUtil.addCookie("username","admin",response);
        CookieUtil.addCookie("password","123456",response);
        CookieUtil.addCookie("name","管理员",response);
    }

}
